import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path"; //这个path用到了上面安装的@types/node
import viteCompression from "vite-plugin-compression"; // 打包压缩
import { changePackageVersion } from "./build/plugins"; // 版本信息
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
const baseConfig = require("./public/baseConfig.json");
export default defineConfig(({ command, mode, ssrBuild }) => {
  return {
    base: baseConfig.baseUrl, // 根路径
    resolve: {
      //这里进行配置别名
      alias: {
        "@": path.resolve("./src"), // @代替src
        "#": path.resolve("./types"), // #代替types
      },
    },
    server: {
      host: "0.0.0.0", //置为 0.0.0.0 或者 true 将监听所有地址
      // port: 8080, //开发服务器端口
      open: true,
      https: false,
      proxy: {
        "^/deviceApi": {
          target: "http://192.168.1.215",
          changeOrigin: true,
        },
        "^/rtspApi": {
          target: "http://192.168.1.215",
          changeOrigin: true,
        },
        "^/HSExtendInterface": {
          target: "http://192.168.1.215",
          changeOrigin: true,
        },
        "^/dapingApi": {
          target: "http://192.168.1.215",
          changeOrigin: true,
        },
        "^/energyApi": {
          target: "http://192.168.1.215",
          changeOrigin: true,
        },
        "^/v1/statistics": {
          target: "http://192.168.1.215",
          changeOrigin: true,
        },
        "^/warnImgApi": {
          target: "http://192.168.1.215",
          changeOrigin: true,
        },
        "^/planAPS": {
          target: "http://192.168.100.12:8097",
          changeOrigin: true,
        },
        // "/v1/carbon": {
        //   target: "http://192.168.100.11:8010",
        //   changeOrigin: true,
        // },
        // "^/three_model": { target: "http://110.185.170.62:8081", changeOrigin: true, },
        // "^/publicData/": { target: "http://192.168.3.246", changeOrigin: true },
        // "^/signalr-hubs/": { target: "http://192.168.3.246", changeOrigin: true },
        // "^/message-center/": { target: "http://192.168.3.246", changeOrigin: true },
        // "^/ssoLogin/": { target: "http://192.168.3.246", changeOrigin: true },
        // "^/time": { target: "http://192.168.3.246", changeOrigin: true },
        // "^/.*?/api/": { target: "http://192.168.3.246", changeOrigin: true },
        // "^/.*?/components/.*?(min.js|min.js\\?v=\\d+)$": { target: "http://192.168.3.246", changeOrigin: true },
        // '^/signalr-notification/signalr-hubs/notifications/realtime/negotiate': { target: "http://192.168.3.246", changeOrigin: true },
        // '^/signalr-notification/signalr-hubs/notifications/realtime\?access_token': { target: "ws://http://192.168.3.246", changeOrigin: true, ws: true },// 代理 websockets 或 socket.io
      },
    },
    build:
      mode === "wc"
        ? {
            target: "esnext",
            // minify: 'terser',
            outDir: "dist/component",
            emptyOutDir: true,
            lib: {
              // Could also be a dictionary or array of multiple entry points
              entry: "src/exports/index",
              // entry: resolve(__dirname, 'lib/main.js'),
              name: "wcLib",
              formats: ["es"], //, 'umd', 'iife', 'cjs'
              // the proper extensions will be added
              fileName: (format, entryName) => {
                return `wc-lib.${format}.js`;
              },
            },
            rollupOptions: {
              // 确保外部化处理那些你不想打包进库的依赖
              // external: ['vue'],
              // output: {
              //   // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
              //   globals: {
              //     vue: 'Vue',
              //   },
              // },
            },
          }
        : undefined,
    define: {
      "process.env": process.env,
    },
    plugins:
      mode === "wc"
        ? [
            // 打包插件
            vue({
              template: {
                compilerOptions: {
                  // 将所有带短横线的标签名都视为自定义元素
                  isCustomElement: (tag) => {
                    return (
                      !["el-", "router-"].some((val) => tag.includes(val)) &&
                      tag.includes("-")
                    );
                  },
                },
              },
            }),
            AutoImport({ resolvers: [ElementPlusResolver()] }),
            Components({ resolvers: [ElementPlusResolver()] }),
            viteCompression(),
          ]
        : [
            vue({
              template: {
                compilerOptions: {
                  // 将所有带短横线的标签名都视为自定义元素
                  isCustomElement: (tag) => {
                    return (
                      !["el-", "router-"].some((val) => tag.includes(val)) &&
                      tag.includes("-")
                    );
                  },
                },
              },
            }),
            AutoImport({ resolvers: [ElementPlusResolver()] }),
            Components({ resolvers: [ElementPlusResolver()] }),
            changePackageVersion(),
          ],
    // css处理
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/assets/styles/element/index.scss" as *;`,
        },
      },
      postcss: {
        plugins: [
          // 处理打包时 "@charset" must be the first rule in the file 警告
          {
            postcssPlugin: "internal:charset-removal",
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === "charset") {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
    },
  };
});
